﻿using System.Diagnostics;
using System;
using System.IO;

namespace BaMusaSoft.RPS.RPSServices
{
    public class Logger
    {
        public Logger(string filePath)
        {
            this.LogFilePath = filePath;
        }
        const string LOGFILENAME = "RPS Log File.log";
        private string LogFilePath
        {
            get;
            set;
        }
        private string LogFileName
        {
            get
            {
                return Path.Combine(LogFilePath, LOGFILENAME);
            }
        }

        public void Error(string message, string source, string methodName)
        {
            WriteEntry(message, "Error", source, methodName);
        }
        public void Error(Exception ex, string source, string methodName)
        {
            WriteEntry(ex.Message, "Error", source, methodName, ex.GetType().Name);
        }
        public void Warning(string message, string source, string methodName)
        {
            WriteEntry(message, "Warning", source, methodName);
        }
        public void Info(string message, string source, string methodName)
        {
            WriteEntry(message, "Info", source, methodName);
        }
        private void WriteEntry(string message, string messageType, string source, string methodName, string exceptionType)
        {
            using (Stream fileStream = new FileStream(LogFileName, FileMode.Append))
            {
                using (TextWriterTraceListener listener = new TextWriterTraceListener(fileStream, "appListener"))
                {
                    Trace.Listeners.Add(listener);

                    Trace.WriteLine(
                       string.Format("{0}, {1} of type: {5}, Source: {2}, Method Name: {3}, {4}",
                       DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                       messageType,
                       source,
                       methodName,
                       message,
                       exceptionType));
                    Trace.Flush();
                    Trace.Close();
                }
            }
        }
        private void WriteEntry(string message, string messageType, string source, string methodName)
        {
            WriteEntry(message, messageType, source, methodName, "");
        }


    }
}
